﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.EntityClient;
using System.Data.Linq;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using QuaManSys;
using System.IO;
using QMSDAL;

namespace QuaManSys.InnerInfo
{
    public partial class InnerInfoDistribute : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断用户是否是非法访问
            if (Session["CurUser"] != null)
            {
                //从session中取出当前用户信息并显示
                CurUser user = (CurUser)Session["CurUser"];

                if (!IsPostBack)
                {
                    Repeater1.DataSourceID = "EntityDataSource1";
                    Repeater1.DataBind();
                    //GetProjectNumbers();
                    lblInnerNo.Text = Request.QueryString["qualityInfoId"];
                    initResponDept();
                    initddlProNo();
                    ShowInfo(lblInnerNo.Text);
                    
                }
            }
            else
            {
                //跳转到登录页面
                Response.Redirect("~/Login.aspx");
            }
        }

        #region 初始化工程编号下拉框

        /// <summary>
        /// 初始化工程编号下拉框
        /// </summary>

        private void initddlProNo()
        {
            DataSet ds = GCBH.GetAllProjectNumbers();
            foreach (DataTable dt in ds.Tables)
            {
                ddlProNo.Items.Clear();
                int count = 0;
                for (int culCol = 0; culCol < dt.Columns.Count; culCol++)
                {
                    for (int culRow = 0; culRow < dt.Rows.Count; culRow++)
                    {
                        ListItem li = new ListItem(dt.Rows[culRow][culCol].ToString());
                        ddlProNo.Items.Insert(count++, li);
                    }
                }
            }

        }
        #endregion

        /// <summary>
        /// 绑定责任部门
        /// </summary>
        private void initResponDept()
        {
            DataSet ds = Department.GetAllDept();
            foreach (DataTable dt in ds.Tables)
            {
                DDLDepRes.Items.Clear();
                int count = 0;
                for (int curCol = 0; curCol < dt.Columns.Count; curCol++)
                {
                    for (int curRow = 0; curRow < dt.Rows.Count; curRow++)
                    {
                        ListItem li = new ListItem(dt.Rows[curRow][curCol].ToString());
                        DDLDepRes.Items.Insert(count++, li);
                    }
                }
            }
        }
        /// <summary>
        ///通过qualityInfoId显示信息
        /// </summary>
        /// <param name="qualityInfoId"></param>
        protected void ShowInfo(string qualityInfoId)
        {
            using (var db = new QuaManSysEntities())
            {
                var query = db.InnerQualityInfo.Find(qualityInfoId);

                //lblNo.Text=query.qualityInfoId;
                txtComponent.Text = query.components;
                ddlProNo.Text = query.projectNo;
                txtMacTyp.Text = GCBH.GetJXGCBH(query.projectNo);
                txtMacNo.Text = GCBH.GetTFGCBH(query.projectNo);
                txtManufac.Text = query.producer;
                txtFaiTyp.Text = query.faultType;
                if (query.projectCategory == "已处理")
                    CheckBox1.Checked = true;
                else CheckBox2.Checked = true;
                txtPjNam.Text = query.name;
                string ss = Convert.ToString(query.deadline);
                txtProDL.Text = ss.Substring(0, 10);  //只保留日期
                txtCustomer.Text = query.customerLinker;
                txtCusTel.Text = query.telephone;
                txtSpeReq.Text = query.specialDemands;
                DDLInfoCla.Text = query.informationSource;
                DDLImpLev.Text = query.levelOfImportance;
                txtInfoDsc.Text = query.description;
                txtInfoWrt.Text = query.registrantName;
                txtInfoDep.Text = query.registrantDepart;
                string ww = Convert.ToString(query.registDate);
                txtWrtDat.Text = ww.Substring(0, 10); //仅保留日期
                txtReqAdv.Text = query.recommendedTreatPlan;
                lblState.Text = query.processingStatus;
                txtReqRsl.Text = query.processingResults;
                txtProCmm.Text = query.processingAssess;
                txtPjCls.Text = query.informationCategory;
                txtInfoWrt.Text = query.registrantName;
                txtInfoDep.Text = query.registrantDepart;
                foreach (var p in query.InnerResponsibleDepart)
                {
                    if (p.isResponDepart == 1)
                    {
                        //DDLDepRes.SelectedItem.Text=p.department;
                        DDLDepRes.Items[0].Text = p.department;
                    }
                }
                string s = "";
                foreach (var p in query.InnerResponsibleDepart)
                {
                    if (p.qualityInfoId == qualityInfoId && p.isResponDepart == 0)
                    {
                        if (s == "") s += p.department;
                        else s += '|' + p.department;

                    }

                }

                FocusDepart.Text = s;

            }
        }


        /// <summary>
        /// 质保部门修改后分发
        /// 修改数据及删除关联数据
        /// 后重新插入关联数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInfoSub_Click(object sender, EventArgs e)
        {

            UpdateInfo(lblInnerNo.Text);
            Response.Redirect("SubmitSuccess.aspx");
        }


        /// <summary>
        /// 删除已有的数据
        /// </summary>
        protected void UpdateInfo(string qualityInfoId)
        {
            using (var db = new QuaManSysEntities())
            {

                var query = (from q in db.InnerQualityInfo
                             where q.qualityInfoId == qualityInfoId
                             select q).Single();


                var query2 = from p in db.InnerResponsibleDepart
                             where p.qualityInfoId == qualityInfoId
                             select p;
                //db.Entry(query).Collection(d => d.InnerResponsibleDepart).Load();

                foreach (var p in query2)
                {
                    db.InnerResponsibleDepart.Remove(p);

                }
                db.SaveChanges();

            }
        }


        /// <summary>
        /// 初始化checkbox
        /// </summary>
        private void initRelatedDept()
        {
            DataSet ds = Department.GetAllDept();
            foreach (DataTable dt in ds.Tables)
            {
                FocusDepart.Text = "";
                cblKeyWords.Items.Clear();
                int count = 0;
                for (int curCol = 0; curCol < dt.Columns.Count; curCol++)
                {
                    for (int curRow = 0; curRow < dt.Rows.Count; curRow++)
                    {
                        if (dt.Rows[curRow][curCol].ToString() == DDLDepRes.Text) continue;
                        ListItem li = new ListItem(dt.Rows[curRow][curCol].ToString());
                        cblKeyWords.Items.Insert(count++, li);
                    }
                }
            }
        }

        protected void btnAttachment_Click(object sender, EventArgs e)
        {
            string path = "~/FileUploadCS/" + lblInnerNo.Text + "/";
            string phyPath = HttpContext.Current.Request.MapPath(path);
            DirectoryInfo upDir = new DirectoryInfo(phyPath);
            if (!upDir.Exists)
            {
                Response.Write("<script>alert('附件不存在')</script>");
                return;
            }
            string wjm = Server.UrlEncode(phyPath);//要进行编码的转换
            //Page.RegisterStartupScript("", " <script   language=javascript> window.open   ( '附件下载.aspx?wjm=" + wjm + " ','下载界面',   'height=300,   width=400,    toolbar=no,   menubar=no,   scrollbars=no,   resizable=no,  location=no,   status=no ') </script> ");
            Page.ClientScript.RegisterStartupScript(GetType(), "", " <script   language=javascript> window.open( 'Innerattachment1.aspx?wjm=" + wjm + " ','下载界面',   'height=300,   width=400,    toolbar=no,   menubar=no,   scrollbars=no,   resizable=no,  location=no,   status=no ') </script> ");


        }

    }
}